package com.cloud.dynamic.gateway.service;


import com.cloud.dynamic.gateway.repository.RedisRouteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Component
public class RedisGatewayRulesService extends GatewayRuleService {

    @Autowired
    protected RedisRouteRepository redisRouteRepository;

    @Override
    public void add(RouteDefinition routeDefinition) {
        redisRouteRepository.save(Mono.just(routeDefinition)).subscribe();
        applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));
    }

    @Override
    public Flux<RouteDefinition> list() {
        return redisRouteRepository.getRouteDefinitions();
    }

    @Override
    public void remove(String dataId) {
        redisRouteRepository.delete(Mono.just(dataId)).subscribe();
    }
}
